package com.itheima.Server;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;

public class MyRunable implements Runnable {

    private ArrayList<String> userinfoList;
    private ArrayList<Socket> socketList;
    private Socket socket;

    public MyRunable(Socket socket,ArrayList<Socket> socketList,ArrayList<String> userinfoList) {
        this.socket = socket;
        this.socketList = socketList;
        this.userinfoList = userinfoList;
    }

    @Override
    public void run() {
        try {
            //到这里就是连接成功了，将结果返回给客户端
            //获取输出流
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write(1);//1表示连接成功
            bw.newLine();
            bw.flush();
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            while (true) {
                //判断用户选择登录还是注册
                String s = br.readLine();
                if(s.equals("1")){
                    //登录
                    System.out.println("开始登录");
                    login();
                }else if(s.equals("2")){
                    //注册
                    System.out.println("开始注册");
                    register();
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void register() throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        //接收客户端的的信息
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println("开始接收用户输入");
        String[] uesrinfo = br.readLine().split("&");
        System.out.println("接收用户输入成功");

        //判断输入是否合法
        String username = uesrinfo[0].split("=")[1];
        System.out.println("获取到用户输入用户名："+username);
        String password = uesrinfo[1].split("=")[1];
        System.out.println("获取到用户输入密码："+password);
        boolean name = username.matches("\\w{6,18}");
        System.out.println("用户名格式判断："+name);
        boolean word = password.matches("\\w\\d{2,7}");
        System.out.println("密码格式判断："+word);
        if(!name){
            bw.write("用户名格式错误");
            bw.newLine();
            bw.flush();
            System.out.println("用户名格式错误");
        }else if(!word){
            bw.write("密码格式错误");
            bw.newLine();
            bw.flush();
            System.out.println("密码格式错误");
        }
        //获取集合内本地userinfo信息
        System.out.println("获取本地文件");
        boolean flag=true;
        for (String s : userinfoList) {
            System.out.println("获取到的信息："+s);
            //拿每一个信息和用户输入的信息进行对比
            String[] trueUesrinfo= s.split("&");
            //比较判断
            if(trueUesrinfo[0].equals(uesrinfo[0])){
                bw.write("用户名重复");
                bw.newLine();
                bw.flush();
                System.out.println("到达用户名重复");
                flag=false;
                break;
            }
        }

        if(name&&word&&flag){
            //格式正确且不重复
            //回写注册成功
            bw.write("注册成功");
            bw.newLine();
            bw.flush();
            System.out.println("格式正确且不重复  注册成功");
            //将用户信息添加到集合
            userinfoList.add(uesrinfo[0]+"&"+uesrinfo[1]);
            System.out.println("此时集合内容："+userinfoList);
            //将用户信息添加到本地文件
            BufferedWriter bw1 = new BufferedWriter(new FileWriter("sockethomework\\serverdir\\userinfo.txt",true));
            bw1.newLine();
            bw1.write("username="+username+"&password="+password);
            bw1.flush();
            System.out.println("用户信息添加到本地文件成功");
            //注册成功后登录
            System.out.println("注册成功开始登录");
            login();
        }

    }

    private void login() throws IOException {
        while (true) {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            //接收客户端的的信息
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            System.out.println("开始接收用户输入");
            String[] uesrinfo = br.readLine().split("&");
            System.out.println("接收用户输入成功");
            //判断正确的用户名和密码
            //获取集合内本地userinfo信息
            System.out.println("获取本地文件");
            boolean flag=false;
            for (String s : userinfoList) {
                System.out.println("获取到的信息："+s);
                //拿每一个信息和用户输入的信息进行对比
                String[] trueUesrinfo= s.split("&");
                //比较判断
                if(trueUesrinfo[0].equals(uesrinfo[0])&&trueUesrinfo[1].equals(uesrinfo[1])){
                    bw.write("登录成功");
                    bw.newLine();
                    bw.flush();
                    flag = true;
                    break;
                }
            }

            //判断是否登陆成功
            if(flag){
                while (true) {
                    //登陆成功
                    //接收客户端的信息
                    System.out.println("开始接收客户端的信息");
                    String talk = br.readLine();
                    System.out.println("接收到"+Thread.currentThread().getName()+"的信息："+talk);
                    //转发给其他客户端
                    //遍历集合
                    System.out.println("开始转发给其他客户端");
                    System.out.println("现在的在线人数："+socketList.size());
                    for (Socket s1 : socketList) {
                        BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(s1.getOutputStream()));
                        bw1.write(Thread.currentThread().getName()+"："+talk);
                        bw1.newLine();
                        bw1.flush();
                    }
                    System.out.println("转发给其他客户端成功");
                }
            }else {
                bw.write("用户名或密码错误");
                bw.newLine();
                bw.flush();
            }
        }
    }
}
